/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.network;

import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Observable;
import net.octopeer.main.OctopeerService;
import net.octopeer.message.Message;

/**
 *
 * @author andria
 */
public class Connection extends Observable implements Runnable {
    /** For reading input from socket */
    private BufferedReader br;

    /** For writing output to socket. */
    private PrintWriter pw;

    /** Socket object representing client connection */

    private Socket socket;
    private boolean running;
    
    private OctopeerService oService;
    private Message message;

    public Connection(Socket socket, OctopeerService oService) throws IOException {
        this.socket = socket;
        this.oService = oService;
        this.message = null;
        
        running = false;
        //get I/O from socket
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
            pw = new PrintWriter(socket.getOutputStream(), true);
            running = true; //set status
        }
        catch (IOException ioe) {
            throw ioe;
        }
    }
	
    /** 
     *Stops clients connection
     */
    public void stopClient() {
        try {
            this.socket.close();
        } catch(IOException ioe){ 
            // unable to stop client
        }
    }

    public void run() {
        String streamInp = ""; //will hold message sent from client
        String command = "";

	//sent out initial welcome message etc. if required...
        pw.println("Welcome to peer");
		
        //start listening message from client//
        try {
            while ((streamInp = br.readLine()) != null && running) {
                //provide your server's logic here//	
                // SERVER SERVICE LOGIC
                String[] tokens = streamInp.split(" ");
                command = tokens[0];
                
                if (command.equals("test")) {
                    System.out.print("from client: " + tokens[1]);
                    pw.println("replay from server: " + tokens[1]); //echo msg back to client//
                } else if (command.equals("request")) {
                    System.out.println("discover request service");
                    oService.requestService(tokens[1]);
                } if (command.equals("respond")) {
                    System.out.println("discover respond service");
                    oService.respondService(tokens[1]);
                }
            }
            running = false;
        } catch (Exception e) {
            running = false;
        }
        
        //it's time to close the socket
        try {
            this.socket.close();
            System.out.println("Closing connection");
        } catch (IOException ioe) { }

        //notify the observers for cleanup etc.
        this.setChanged();              //inherit from Observable
        this.notifyObservers(this);     //inherit from Observable
    }
}